import { decryptByDESModeEBC, encryptByDESModeEBC } from '@/common/encrypt';
import http from '@/common/phoneOrderHttp';
import Utils from '@/common/pakjUtils';
import weixin from '@/common/weixinUtils';
import Config from '@/common/config';
import { Dialog, Toast } from 'mand-mobile';
import Loading from '@/components/loading';
var CryptoJS = require('crypto-js');
const defaultState = () => {
  return {
    orderlist: [],
    phoneOrderInfo: null,
    h5PhonePaymentInfo: null
  };
};
const state = defaultState();
const getters = {
  phoneOrderList(state) {
    return state.orderlist;
  },
  getPhoneOrderInfoData(state) {
    return state.phoneOrderInfo;
  },
  getH5honePaymentInfo(state) {
    return state.h5PhonePaymentInfo;
  }
};
const actions = {
  getPhoneOrdersByPhone(context, phone) {
    return new Promise((resolve, reject) => {
      Loading.show('正在查询订单');
      let params = {
        userPhone: encryptByDESModeEBC(phone)
      };
      http.post('pakj/order/getOrderListByPhone', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('setOrderList', res.data.data.order);
          resolve();
        } else {
          reject(new Error(res.data.msg));
        }
      });
    });
  },
  getPhoneOrderInfo(context, orderId) {
    return new Promise((resolve, reject) => {
      http
        .post('pakj/order/getPhoneOrderPayInfo', {
          order_no: orderId
        })
        .then(res => {
          if (res.data.code === '000000') {
            context.commit('setPhoneOrderInfo', res.data.data);
            resolve();
          } else {
            Dialog.alert({
              title: '错误',
              content: res.data.msg,
              confirmText: '确定'
            });
            reject(new Error(res.data.code + ':' + res.data.msg));
          }
        });
    });
  },
  h5PhonePayment(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/order/toPayOrderH5', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('setH5PhonePaymentInfo', res.data.data);
          resolve();
        } else if (res.data.code === '200008') {
          Dialog.alert({
            title: '错误',
            content: '请不要重复支付',
            onConfirm: () => {
              reject(new Error('200008'));
            }
          });
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  phoneOrderWxAuth(context, code) {
    return new Promise((resolve, reject) => {
      Loading.show();
      let params = {
        code: code
      };
      http
        .post('pakj/wexin/phoneOrderAuth', params)
        .then(res => {
          Loading.hide();
          if (res.data.code === '000000') {
            resolve(res.data.data);
          } else {
            reject(new Error(res.data.msg));
          }
        })
        .catch(reason => {
          Loading.hide();
          reject(reason);
        });
    });
  },
  // 微信支付成功后 查询订单支付状态
  paymentPhoneStatus(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/userorder/payResultQuery', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          if (res.data.data.code === '1') {
            resolve();
          } else if (res.data.data.code === '2') {
            // 订单未支付
            reject(new Error(res.data.data.code + ':' + res.data.data.msg));
          } else if (res.data.data.code === '5') {
            // 等待支付
            reject(new Error(res.data.data.code + ':' + res.data.data.msg));
          } else if (res.data.data.code === '4') {
            // 支付失败
            reject(new Error(res.data.data.code + ':' + res.data.data.msg));
          }
        }
      });
    });
  },
  phoneOrderWxPayment(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/order/toPayOrder', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('WXsetPaymentInfo', res.data.data);
          resolve();
        } else if (res.data.code === '200008') {
          Dialog.alert({
            title: '错误',
            content: '请不要重复支付',
            onConfirm: () => {
              reject(new Error('200008'));
            }
          });
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  }
};
const mutations = {
  setOrderList(state, list) {
    state.orderlist = list;
  },
  setPhoneOrderInfo(state, data) {
    console.log('得到的电话约单订单信息：', data);
    state.phoneOrderInfo = data;
  },
  setH5PhonePaymentInfo(state, data) {
    state.h5PhonePaymentInfo = data;
  }
};
export default {
  state,
  actions,
  getters,
  mutations
};
